package tij4.concurrent.source;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/*
    生产者和消费者
 */
class Meal {
    private final int orderNum;

    public Meal(int orderNum) {
        this.orderNum = orderNum;
    }

    @Override
    public String toString() {
        return "Meal" + orderNum;
    }
}

class WaitPersion implements Runnable {
    private Restaurant restaurant;

    public WaitPersion(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try{
            while (!Thread.interrupted()) {
                System.out.println("==========");   //在执行chef.run()的线程进入休眠时此处可以执行两次
                synchronized (this) {
                    while (restaurant.meal == null) {
                        System.out.println("wait()前");
                        wait();
                        System.out.println("wait()后");
                    }
                    System.out.println("WaitP.run()拥有waitP锁");
                }
                System.out.println("Waitpersion got " + restaurant.meal);
                synchronized (restaurant.chef) {
                    restaurant.meal = null;
                    restaurant.chef.notifyAll();
                    System.out.println("WaitP.run()拥有chef锁");
                }
                System.out.println("WaitPersion " + Thread.currentThread());
            }

        }catch (InterruptedException e) {
            System.out.println("WaitPersion interrupted");
        }
    }
}

class Chef implements Runnable {
    private Restaurant restaurant;
    private int count = 0;

    public Chef(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal != null)
                        wait();
                }
                if (++count == 10) {
                    System.out.println("Out of food, closing");
                    restaurant.exec.shutdownNow();
                }
                System.out.println("Order up!");
                synchronized (restaurant.waitPersion) {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPersion.notifyAll();
                    System.out.println("notifyAll()已经调用,后面会紧跟着wait()吗");
                }
                System.out.println("Chef " + Thread.currentThread());
                TimeUnit.MILLISECONDS.sleep(10000);
            }
        }catch (InterruptedException e) {
            System.out.println("Chef interrrupted");
        }
    }
}

public class Restaurant {
    Meal meal;
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPersion waitPersion = new WaitPersion(this);
    Chef chef = new Chef(this);

    public Restaurant() {
        exec.execute(chef);
        exec.execute(waitPersion);
    }

    public static void main(String[] args) {
        new Restaurant();
    }
}
